package com.jt.web.controller;

import com.jt.web.Vo.SysResult;
import com.jt.web.pojo.User;
import com.jt.web.service.UserService;
import com.jt.web.utils.ResultUtil;
import com.jt.web.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public String doLogin(@RequestBody User user){
        if (StringUtils.isEmpty(user.getUserName()) && StringUtils.isEmpty(user.getPassWord()))
            return ResultUtil.errorWithMessage("用户名或密码错误");
        String token = userService.Login(user);
//        if(token == null)
//            return ResultUtil.errorWithMessage("用户名或密码错误");
        return ResultUtil.successWithData(token);
    }
    //用户注册功能
    @PostMapping("/register")
    public String doRegister(@RequestBody User user) {
        if(StringUtils.isEmpty(user.getUserName())|| StringUtils.isEmpty(user.getPassWord())) {
            throw new RuntimeException("参数错误！");
        }
        String in = userService.register(user);
        return in;
    }
}
